/*
 * ViewerListener.cpp
 *
 *  Created on: May 25, 2011
 *      Author: madboyka
 */

#include "ViewerListener.h"
#include "ViewerProtocol.h"
#include "ViewerProtocolFactory.h"
#include "Logger.h"

ViewerListener::ViewerListener(Server* server)
{
	this->server = server;
}

ViewerListener::~ViewerListener()
{
}

void ViewerListener::run()
{
	QString protocolType = Server::getSetting("ViewerProtocol","SdlSocketTcp").toString();
	Logger::log("Viewer is using the %s implementation.", protocolType.toStdString().data());
	protocol = ViewerProtocolFactory::create(protocolType);
	try
	{
		protocol->bind();
		Logger::log("Viewer protocol bound!");
	} catch (NetworkException ex)
	{
		Logger::error(ex.showReason());
	}
	while (true)
	{
		try
		{
			ViewerProtocol *p = protocol->acceptConnection();
			Logger::log("Viewer connected!");
			server->addViewer(p);
		} catch (NetworkException ex)
		{
			Logger::log(ex.showReason());
		}
	}
}
